from Pocket.menu import display_menu, get_number_input
from Pocket.file_manager import create_file_if_not_exists, check_file_exists, save_to_file, load_from_file, count_lines
from Pocket.id_manager import create_Pokémon
from Pocket.Pokémon_manager import display_Pokémon, release_Pokémon

def main():
    Pokémon = [] 
    FILENAME = "pokémon_collection.txt"
    create_file_if_not_exists(FILENAME)
    if count_lines(FILENAME) == 0:
        print("No existing Pokémon collection found. Creating a new one.")
        create_Pokémon(Pokémon, "Pikachu", "Electric", 5)
        create_Pokémon(Pokémon, "Charmander", "Fire", 8)
        create_Pokémon(Pokémon, "Terracotta Army", "ground", 999)
        save_to_file(Pokémon, FILENAME)
    
    if check_file_exists(FILENAME):
        Pokémon = load_from_file(FILENAME)
    while True:
        display_menu()
        choice = get_number_input("choice(1-4): ")
        
        if choice == '1':
            display_Pokémon(Pokémon)
        elif choice == '2':
            name = input("Enter Pokémon name: ")
            type = input("Enter Pokémon type: ")
            level = int(input("Enter Pokémon level: "))
            create_Pokémon(Pokémon, name, type, level)
            print(f"✅ Pokémon {name} added successfully!")
        elif choice == '3':
            Pokémon_id = input("Enter the ID of the Pokémon to release: ")
            Pokémon = release_Pokémon(Pokémon, Pokémon_id)
        elif choice == '4':
            save_to_file(Pokémon, FILENAME)
            print("Goodbye!")
            break
        else:
            print("❌ Invalid choice, please try again.")
if __name__ == "__main__":
    print("Welcome to Pocket Monsters!")
    main()
    print("Thank you for using Pocket Monsters!")